package com.swing.games.snake.thread;

import base.RunnableBase;
import com.swing.assets.Iurl;

import javax.swing.*;
import java.net.URL;
import java.util.Random;

/**
 * @author Yuang, Gongming
 * @date 2022-02-14 15:40
 */
public class Dynamichead extends ImageIcon {

    private static Dynamichead dynamichead;

    private Dynamichead() {
    }

    public static Dynamichead init() {
        if (dynamichead == null) {
            synchronized (Dynamichead.class) {
                if (dynamichead == null) {
                    dynamichead = new Dynamichead();
                    new Thread(()->{
                        //  线程中开启随机类
                        Random random = new Random();
                        //  一直循环下去
                        while(true){
                            try{
                                ImageIcon imageIcon = new ImageIcon(new URL(Iurl.LOAD_2[random.nextInt(Iurl.FRUIT.length - 1)]));
                                dynamichead.setImage(imageIcon.getImage());
                                Thread.sleep(100);
                            }catch (Exception e){
                                //
                            }
                        }
                    },"贪吃蛇头细节").start();
                }
            }
        }
        return dynamichead;
    }

}
